package com.profile.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.profile.domain.Profile;
import com.profile.domain.SearchCriteria;
import com.profile.service.ProfileService;

@Service
public class MockProfileService implements ProfileService {
	private static Map<Integer, Profile> profiles = new HashMap<Integer, Profile>();
	private static int nextProfileId = 5;

	private static Date parseDate(String textDate) {
		try {
			return new SimpleDateFormat("dd-MM-yyyy").parse(textDate);
		} catch (ParseException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	static {
		HashSet<String> ListSkill1 = new HashSet<String>();
		ListSkill1.add("Java Web development (JSP,servlet,applet)");
		ListSkill1.add("JavaEX");
		profiles.put(1, new Profile(1, "Lam", "Tran", parseDate("16-09-1990"),
				true, "ASE", ListSkill1));
		HashSet<String> ListSkill2 = new HashSet<String>();
		ListSkill2.add(".NET programming");
		profiles.put(2, new Profile(2, "Minh", "Do", parseDate("21-09-1990"),
				true, "ASE", ListSkill2));
		HashSet<String> ListSkill3 = new HashSet<String>();
		ListSkill3.add("Portlet/Portal");
		profiles.put(3, new Profile(3, "Thanh", "Nguyen", parseDate("27-10-1990"),
				true, "ASE", ListSkill3));
		HashSet<String> ListSkill4 = new HashSet<String>();
		ListSkill4.add("C/C++");
		profiles.put(4, new Profile(4, "Khanh", "Le", parseDate("12-02-1985"),
				false, "SE", ListSkill4));
		HashSet<String> ListSkill5 = new HashSet<String>();
		ListSkill5.add("C/C++");
		profiles.put(5, new Profile(5, "Le", "Bui", parseDate("12-12-1982"),
				true, "PSE", ListSkill5));
	}

	public Collection<Profile> getAllProfiles() {
		return profiles.values();
	}

	public Collection<Profile> searchProfiles(SearchCriteria searchCriteria) {
		Collection<Profile> Results;
		Results = getProfileByFirstName(searchCriteria.getFirstName(),
				profiles.values());
		Results = getProfileByLastName(searchCriteria.getLastName(), Results);
		Results = getProfileByTittle(searchCriteria.getTittle(), Results);
		if (searchCriteria.getSkill().iterator().hasNext()) {
			Results = getProfileBySkill(searchCriteria.getSkill().iterator().next(),
					Results);
		}
		return Results;
	}

	public Profile getProfileById(int profileId) {
		Profile profile = profiles.get(profileId);
		return new Profile(profile.getID(), profile.getFirstName(),
				profile.getLastName(), profile.getDOB(), profile.getGender(),
				profile.getTittle(), profile.getSkill());
	}

	private Collection<Profile> getProfileByFirstName(String profileFirstName,
			Collection<Profile> ListProfile) {
		if (profileFirstName == null || profileFirstName == "")
			return ListProfile;
		HashSet<Profile> Results = new HashSet<Profile>();
		profileFirstName = profileFirstName.replaceAll("^\\s+", "");
		profileFirstName = profileFirstName.replaceAll("\\s+$", "");
		profileFirstName = profileFirstName.replaceAll("\\b\\s{2,}\\b", " ");
		for (Profile profile : ListProfile) {
			if (profile.getFirstName().toLowerCase()
					.matches("(.*)" + profileFirstName.toLowerCase() + "(.*)")) {
				Results.add(profile);
			}
		}
		return Results;
	}

	private Collection<Profile> getProfileByLastName(String profileLastName,
			Collection<Profile> ListProfile) {
		if (profileLastName == null || profileLastName == "")
			return ListProfile;
		HashSet<Profile> Results = new HashSet<Profile>();
		profileLastName = profileLastName.replaceAll("^\\s+", "");
		profileLastName = profileLastName.replaceAll("\\s+$", "");
		profileLastName = profileLastName.replaceAll("\\b\\s{2,}\\b", " ");
		for (Profile profile : ListProfile) {
			if (profile.getLastName().toLowerCase()
					.matches("(.*)" + profileLastName.toLowerCase() + "(.*)")) {
				Results.add(profile);
			}
		}
		return Results;
	}

	private Collection<Profile> getProfileByTittle(String profileTittle,
			Collection<Profile> ListProfile) {
		if (profileTittle == null || profileTittle == "")
			return ListProfile;
		HashSet<Profile> Results = new HashSet<Profile>();
		for (Profile profile : ListProfile) {
			if (profile.getTittle().toLowerCase()
					.equals(profileTittle.toLowerCase())) {
				Results.add(profile);
			}
		}
		return Results;
	}

	private Collection<Profile> getProfileBySkill(String profileSkill,
			Collection<Profile> ListProfile) {
		if (profileSkill == null || profileSkill == "")
			return ListProfile;
		HashSet<Profile> Results = new HashSet<Profile>();
		for (Profile profile : ListProfile) {
			for (String skill : profile.getSkill()) {
				if (skill.toLowerCase().equals(profileSkill.toLowerCase()))
					Results.add(profile);
			}
		}
		return Results;
	}

	public void saveProfile(Profile profile) {
		if (profile.IsNew()) {
			nextProfileId++;
			profile.setID(nextProfileId);
			profiles.put(nextProfileId, profile);
		} else {
			profiles.put(profile.getID(), profile);
		}

	}

	public void deleteProfile(Integer Id) {
		profiles.remove(Id);
	}

}
